package com.yhyvr.console.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.StringUtil;
import com.lz.tool.UUIDTool;
import com.lz.tool.http.LzGet;
import com.yhyvr.console.annotation.RepeatSubmit;
import com.yhyvr.console.service.NewsHtmlReplaceTool;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lz.kit.LogKit;
import com.lz.tool.LzStringUtils;
import com.yhyvr.pojo.resp.console.BootStrapGridResp;
import com.yhyvr.console.model.login.LoginUserModel;
import com.lz.mybatis.jdbc.auto.dao.BoWebNewsMapper;
import com.lz.mybatis.jdbc.auto.model.BoWebNews;
import com.lz.mybatis.jdbc.auto.model.BoWebNewsExample;
import com.yhyvr.pojo.resp.CommonResp;
import com.yhyvr.pojo.resp.HttpResult;
import com.yhyvr.pojo.req.console.BootStrapGridReq;
import com.yhyvr.console.controller.ControllerQueryTool;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * title:
 * </p>
 * <p>
 * description:
 * </p>
 *
 * @create 张钦
 * @update
 * @version 1.0
 * @date
 */
@Controller
public class BoWebNewsController{
	@Resource
	private BoWebNewsMapper modelMapper;
	@Resource
	private NewsHtmlReplaceTool newsHtmlReplaceTool;
	@RequestMapping("/console/bo_web_news/add/{type}")
	@RepeatSubmit(isAdd = true)
	public String add(@PathVariable int type,ModelMap modelMap ){
		BoWebNews boWebNews = new BoWebNews();
		boWebNews.setNewsType(type);
		modelMap.put("boWebNews",boWebNews);
		return "console/boWebNews/boWebNews_form";
	}

	@RepeatSubmit(isAdd = true)
	@RequestMapping("/console/bo_web_news/edit")
	public String edit(ModelMap modelMap,String id){
			modelMap.put("boWebNews", modelMapper.selectByPrimaryKey(id));
		return "console/boWebNews/boWebNews_form";
	}

	@RequestMapping("/console/bo_web_news/list")
	public String list(){
		return "console/boWebNews/boWebNews_table";
	}
	@RequestMapping("/console/bo_web_news/list/problem")
	public String problem(ModelMap modelMap){
		modelMap.put("newsType",1);
		return "console/boWebNews/boWebNews_table";
	}
	@RequestMapping("/console/bo_web_news/list/{type}")
	public String type(ModelMap modelMap,@PathVariable int type){
		modelMap.put("newsType",type);
		return "console/boWebNews/boWebNews_table";
	}
	@RequestMapping("/console/bo_web_news/json")
	public @ResponseBody BootStrapGridResp json(BootStrapGridReq bootGridReq){
		Page page = PageHelper.offsetPage(bootGridReq.getOffset(), bootGridReq.getLimit());
    	if(bootGridReq.getSort()!=null) {
    		page.setOrderBy(LzStringUtils.chageStringUpCaseAnd_(bootGridReq.getSort()) + " " + bootGridReq.getOrder());
    	}else{
    		page.setOrderBy("create_time desc");
		}
		BoWebNewsExample boWebNewsExample = new BoWebNewsExample();
        ControllerQueryTool.setBoWebNewsCriteria(bootGridReq.getQuery(),boWebNewsExample.createCriteria());
		List<BoWebNews> boWebNewsList = modelMapper.selectByExample(boWebNewsExample);
		return new BootStrapGridResp(page.getTotal(),boWebNewsList);
	}

	@RepeatSubmit(isAdd = false)
	@RequestMapping("/console/bo_web_news/save")
	public @ResponseBody HttpResult save(String formObjectJson){
		try{
		BoWebNews boWebNews= JSON.parseObject(formObjectJson,BoWebNews.class);
			String id=boWebNews.getId();
			String content = boWebNews.getContent();
			String html = newsHtmlReplaceTool.getAliYunImageHtml(content);
			boWebNews.setContent(html);
		if (id!=null) {
			boWebNews.setUpdateTime(new Date());
			modelMapper.updateByPrimaryKeySelective(boWebNews);
			return CommonResp.getSuccess();
		}else{
			boWebNews.setId(UUIDTool.getUUID());
			boWebNews.setCreateTime(new Date());
			modelMapper.insertSelective(boWebNews);
			try {
				new LzGet().get("http://www.arbanban.com/baidu/seo");
			}catch (Exception e){
				LogKit.error(e.getMessage(),e);
			}

			return CommonResp.getSuccess();
		}

		}catch(Exception e){
			LogKit.error(e.getMessage());
			return CommonResp.getError();
		}

	}

	@RequestMapping("/console/bo_web_news/delete")
	public @ResponseBody HttpResult delete(String ids){
		String[]idsa=ids.split(",");
		for (String id : idsa) {
		modelMapper.deleteByPrimaryKey(String.valueOf(id));
		}
		return CommonResp.getSuccess();
	}
}
